<?php
/**
 * @author Jumper Swordwave
 * date: 2021/04/19
 */
declare(strict_types=1);
namespace App\Game\Core;

use App\Constants\ErrorCode;
use App\Exception\RouteException;

class Route
{
    /**
     * 路由
     */
    private $routes;

    /**
     * 命名空间
     */
    private $namespace;

    public function __construct()
    {
        $this->namespace = 'App\Game\Commands\\';
        $this->routes = [
            Command::SYS_CONNECTED => 'SysCommand@connected',
            Command::SYS_HEART_REQ => 'SysCommand@heart',
            Command::SYS_LOGIN => 'SysCommand@login',
            Command::SYS_LOGOUT => 'SysCommand@logout',
            Command::SYS_RE_CONNECT_REQ => 'SysCommand@reconnect',

            Command::GAME_START_REQ => 'GameCommand@start',
            Command::GAME_CALL_REQ => 'GameCommand@call',
            Command::GAME_OUT_REQ => 'GameCommand@outcard',

            Command::CHAT_MSG_REQ => 'ChatCommand@chat',
        ];
    }

    public function match(int $command)
    {
        if (isset($this->routes[$command])) {
            list($class, $action) = explode('@', $this->routes[$command]);

            return [$this->namespace . $class, $action];
        }

        throw new RouteException(ErrorCode::SERVER_NOTFOUND, '请求目标不存在,command:' . $command);
    }
}